package com.rerun.common.jpush;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * The type Jpush operations.
 *
 * @author dax.
 * @version v1.0
 * @since 2017 /11/6 16:00
 */
public class JpushOperations implements Operations {
    private static final Logger log = LoggerFactory.getLogger("Jpush");
    private JPushClient jPushClient;


    @Override
    public PushResult pushAllAlter(String msg) {
        PushPayload payload = buildPushAlterAll(msg);
        return sendMsg(payload);
    }

    @Override
    public PushResult pushAllHidden(String msg) {
        PushPayload payload = buildPushObjectAll(msg);
        return sendMsg(payload);
    }

    @Override
    public PushResult pushAllHiddenByRegistrationId(String[] registrationIds,String msgTitle, String msg) {
        PushPayload payload = buildPushObjectRegistIds(registrationIds,msgTitle, msg);
        return sendMsg(payload);
    }

    @Override
    public PushResult pushAllAlertByRegistrationId(String[] registrationIds, String msg) {
        PushPayload payload = buildPushAlertRegistIds(registrationIds, msg);
        return sendMsg(payload);
    }

    @Override
    public PushResult pushAlertByAlias(List<String> aliases, String msg) {
        PushPayload payload = buildPushAlertAlias(aliases, msg);
        return sendMsg(payload);
    }

    @Override
    public PushResult pushAlertByTags(List<String> tags, String msg) {
        PushPayload payload = buildPushAlertTags(tags, msg);
        return sendMsg(payload);
    }

    private PushResult sendMsg(PushPayload payload) {
        PushResult pushResult = null;
        try {
            pushResult = jPushClient.sendPush(payload);
        } catch (APIConnectionException e) {
            log.error("jpush connection error", e);
        } catch (APIRequestException e) {
            log.info("HTTP状态码: {} 错误代码: {} 报错信息: {}", e.getStatus(), e.getErrorCode(), e.getErrorMessage());
        }
        return pushResult;
    }

    private static PushPayload buildPushObjectAll(String msgContent) {
        return PushPayload.newBuilder().setPlatform(Platform.all())// 设置接受的平台，安卓和iso
                .setAudience(Audience.all())// 所有人
                .setMessage(Message.content(msgContent))//自定义消息，不会在前台弹出
                .build();
    }

    private static PushPayload buildPushAlterAll(String msgContent) {
        return PushPayload.alertAll(msgContent);
    }

    private static PushPayload buildPushObjectRegistIds(String[] reIds,String msgTitle, String msgContent) {
        return PushPayload.newBuilder().setPlatform(Platform.all())// 设置接受的平台，安卓和iso
                .setAudience(Audience.registrationId(reIds))// registIds推送
                .setMessage(Message.newBuilder().setTitle(msgTitle).setMsgContent(msgContent).build())//自定义消息，不会在前台弹出
                .build();
    }

    /**
     * 极光消息的消息体   Notification  Message  必须唯一
     *
     */
    private static PushPayload buildPushAlertRegistIds(String[] reIds, String msgContent) {
        return PushPayload.newBuilder().setPlatform(Platform.all())// 设置接受的平台，安卓和iso
                .setAudience(Audience.registrationId(reIds))// registIds推送
                .setNotification(Notification.alert(msgContent))
                .build();
    }

    private static PushPayload buildPushAlertTags(List<String> tags, String msgContent) {
        return PushPayload.newBuilder().setPlatform(Platform.all())// 设置接受的平台，安卓和iso
                .setAudience(Audience.tag(tags))// tags推送
                .setNotification(Notification.alert(msgContent))
                .build();
    }

    private static PushPayload buildPushAlertAlias(List<String> aliases, String msgContent) {
        return PushPayload.newBuilder().setPlatform(Platform.all())// 设置接受的平台，安卓和iso
                .setAudience(Audience.alias(aliases))// Alias推送
                .setNotification(Notification.alert(msgContent))
                .build();
    }

    public JPushClient getjPushClient() {
        return jPushClient;
    }

    public void setjPushClient(JPushClient jPushClient) {
        this.jPushClient = jPushClient;
    }
}
